package com.kurumi.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.pojo.RespondResult;
import com.kurumi.query.MedicalRecordQualityControlQuery;
import com.kurumi.service.MedicalRecordQualityControlService;
import com.kurumi.service.MedicalRecordTraceService;
import com.kurumi.util.ExcelUtil;

@Controller
@RequestMapping("/medical_record_quality_control")
public class MedicalRecordQualityControlController {

	@Autowired
	private MedicalRecordQualityControlService qualityControlService;
	
	@Autowired
	private MedicalRecordTraceService medicalRecordTraceService;
	
	
	@GetMapping("/quality_control_page")
	public String qualityControlPage(){
		return "quality_control/quality_control_page";
	}
	
	@GetMapping("/quality_control_score_page")
	public String qualityControlScorePage(String visitGuid,Model model){
		model.addAttribute("visitGuid",visitGuid);
		try{
			medicalRecordTraceService.insertQualityControlPageTrace(visitGuid);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return "quality_control/quality_control_score_page";
	}
	
	@GetMapping("/office_control_record")
	public String officeControlRecord(MedicalRecordQualityControlQuery params,Model model){
		params.setPageSize(null);
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			int count = 0;
			medicalRecords = qualityControlService.getQualityControlRecord(params);
			count = medicalRecords.size();
			params.setTotalCounts(count);
			params.setQueryDatas(medicalRecords);
			model.addAttribute("params", params);
			if(params.getMaxScore() != null && params.getMinScore() != null){
				if(params.getMinScore().floatValue()==89.5 && params.getMaxScore().floatValue() == 100){
					model.addAttribute("title", "甲级病案");
				}else if(params.getMinScore().floatValue()==74.5 && params.getMaxScore().floatValue() == 89.5){
					model.addAttribute("title", "乙级病案");
				}else{
					model.addAttribute("title", "丙级病案");
				}
			}else{
				model.addAttribute("title", "病案");
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return "quality_control/office_control_record";
	}
	
	@GetMapping("/query_quality_control_imperfection")
	public String queryQualityControlImperfection(Model model){
		return "quality_control/query_quality_control_imperfection";
	}
	
	@GetMapping("/ajax_query_quality_control_imperfection")
	@ResponseBody
	public RespondResult ajaxQueryQualityControl(MedicalRecordQualityControlQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			int count = 0;
			if(!params.emptyOutDeptCodeAndOutDateAndMrIdAndQualityControlDesc()){
				if(params.getQualityControlDesc()!= null){
					params.setQualityControlDesc("%"+params.getQualityControlDesc()+"%");
				}
				medicalRecords = qualityControlService.getQualityControlImperfection(params);
				count = medicalRecords.size();
			}
			
			params.setTotalCounts(count);
			params.setQueryDatas(medicalRecords);
			respondResult = new RespondResult(true, RespondResult.successCode, null, params);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), params);
		}
		
		return respondResult;
	}
	
	@GetMapping("/imperfection_to_excel")
	public void imperfectionToExcel(MedicalRecordQualityControlQuery params,HttpServletResponse response) {
		String[] headers = {  "住院号","姓名","出院科室", "出院日期","评分项", "缺陷描述", "扣除分数" };
		OutputStream out = null;
		List<String> keys = new ArrayList<String>();
		keys.add("only_id");
		keys.add("patient_name");
		keys.add("out_dept_name");
		keys.add("out_hospital_date_format");
		
		keys.add("third_level_name");
		keys.add("remark");
		keys.add("deduction");
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.emptyOutDeptCodeAndOutDateAndMrId()){
				medicalRecords = qualityControlService.getQualityControlImperfection(params);
			}
			response.setContentType("octets/stream");
			String title= "质控缺陷导出";
			StringBuilder downLoadFileName = new StringBuilder();
			downLoadFileName = new StringBuilder(
					"attachment;filename=");
			downLoadFileName.append(title)
					.append(".xls");
			String encodeStr = new String(downLoadFileName.toString().getBytes(
					"GB2312"), "ISO_8859_1");
			response.addHeader("Content-Disposition", encodeStr);
			out = response.getOutputStream();
			ExcelUtil.exportExcel(out, title, headers, keys, medicalRecords);
			
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(out != null){
				try {
					out.flush();
				} catch (Exception e2) {
					// TODO: handle exception
				}
				try {
					out.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
		}
		
	}
	
	
	
}
